//
//  MyMessageViewController.m
//  suRead
//
//  Created by Kobe LI on 16/4/27.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "MyMessageViewController.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "ServiceNews.h"
#import "AccountManager.h"
#import "UIViewController+HUD.h"
#import "StationMessageCell.h"
#import "M_Letter.h"


@interface MyMessageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UIView *topBarView;

/**
 *  @brief 站内私信数组
 */
@property (nonatomic,strong)NSArray *letterArray;

@property (nonatomic,strong)UITableView *CommentTableView;

@end

@implementation MyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.letterArray  = [NSArray new];
    
    __weak typeof(self) weak_self = self;
    self.view.backgroundColor = [UIColor whiteColor];
    self.topBarView = [UIView new];
    self.topBarView.backgroundColor = [UIColor colorWithRed:248/255.0f green:248/255.0f blue:248/255.0f alpha:1.0f];
    [self.view addSubview:self.topBarView];
    [self.topBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weak_self.topBarView.superview);
        make.height.equalTo(@64.0f);
    }];
    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"button_back"] forState:UIControlStateNormal];
    [buttonBack addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        //返回
        [weak_self.navigationController popViewControllerAnimated:YES];
    }];
    [weak_self.topBarView addSubview:buttonBack];
    
    [buttonBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(weak_self.topBarView);
        make.size.mas_equalTo(CGSizeMake(44.0f, 44.0f));
    }];
    UILabel *titleLabel = [UILabel new];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"我的消息";
    titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.f];
    [self.topBarView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weak_self.topBarView);
        make.bottom.equalTo(weak_self.topBarView.mas_bottom).with.offset(-10.0f);
    }];
    
    
    self.CommentTableView = [UITableView new];
    self.CommentTableView.delegate = self;
    self.CommentTableView.dataSource = self;
    self.CommentTableView.tableFooterView = [UIView new];
    [self.view addSubview:self.CommentTableView];
    [self.CommentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weak_self.topBarView.mas_bottom);
        make.left.equalTo(weak_self.CommentTableView.superview.mas_left);
        make.right.equalTo(weak_self.CommentTableView.superview.mas_right);
        make.bottom.equalTo(weak_self.CommentTableView.superview.mas_bottom);
    }];
    self.CommentTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [self.CommentTableView registerNib:[UINib nibWithNibName:@"StationMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"StationMessageCell"];

    [self showHUDProgress:@""];
    [ServiceNews doFetchPrivateLetter:^(id responseObject, NSString *message)
     {
         [self removeHUDProgress];
         self.letterArray = responseObject;
         if (self.letterArray.count > 0) {
             [self.CommentTableView reloadData];
         } else {
             [self showHUDText:@"暂无私信"];
         }
     }
                              failure:^(NSString *message)
     {
         [self removeHUDProgress];
         [self showHUDText:message];
     }];

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.letterArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 138.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    [cell setNeedsLayout];
    [cell layoutIfNeeded];
    
    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height + 1.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StationMessageCell *cell = (StationMessageCell *)[tableView dequeueReusableCellWithIdentifier:@"StationMessageCell"];
    M_Letter *letterData = [self.letterArray objectAtIndex:indexPath.row];
    [cell.stationMessageTitleLabel setText:letterData.f_title];
    [cell.stationMessageComentLabel setText:letterData.f_addtime];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
